package cn.yuhao.goodbook.user.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.jdbc.TxQueryRunner;
import cn.yuhao.goodbook.user.domain.User;


public class UserDao {
	private QueryRunner qr=new TxQueryRunner();
	/*
	 * 对用户名进行校验
	 */
	public boolean ajaxValidateLoginname(String loginname){
		String sql="select count(1) from t_user where loginname=?";
		try {
			Number number=(Number) qr.query(sql, new ScalarHandler(),loginname);
			return number.intValue()==0;
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	public boolean ajaxValidateEmail(String email){
		String sql="select count(1) from t_user where email=?";
		try {
			Number number=(Number) qr.query(sql, new ScalarHandler(),email);
			return number.intValue()==0;
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	public void add(User user){
		String sql="insert into t_user values(?,?,?,?,?,?)";
		Object[] params={user.getUuid(),user.getLoginname(),user.getLoginpass(),user.getEmail(),user.getStatus(),user.getActivationCode()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
}
